﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIA.Presentation.Contracts;
using VIA.Presentation;
using VIA.Project.Presentation.ViewModel.Inspection;
using VIA.SiMoJi.Domain.Inspection;
using PropertyChanged;
using VIA.Integration;
using VIA.SiMoJi.Domain.Electronics;
using OpenTK.Graphics.ES10;
using VIA.Integration.Presentation;
using VIA.SiMoJi.Domain.Option;
using System.Windows.Media.Imaging;
using VIA.Foundation;

namespace VIA.SiMoJi.Presentation.ViewModel
{
    [ViewModel(typeof(IGongLiaoStationOptionView))]
    internal class GongLiaoStationOptionViewModel : EditorViewModel<IGongLiaoStationOptionView, GongLiaoStationOption>
    {
        private bool isRunning;
        private readonly Inspector _inspector;
        private readonly BuJinPiDai _station;
        private CylinderDebugViewModel cM_阻挡气缸1;
        private CylinderDebugViewModel cM_阻挡气缸2;
        private CylinderDebugViewModel cM_左右气缸1;
        private CylinderDebugViewModel cM_左右气缸2;
        private CylinderDebugViewModel cM_前后气缸1;
        private CylinderDebugViewModel cM_前后气缸2;
        private CylinderDebugViewModel cM_上下气缸1;
        private CylinderDebugViewModel cM_上下气缸2;
        private double speed;

        private GongLiaoStation GongLiaoStation { get; }

        public IRelayCommand PMoveCommand { get; }
        public IRelayCommand NMoveCommand { get; }
        public IRelayCommand StopCommand { get; }
        public CylinderDebugViewModel CM_阻挡气缸1 { get => cM_阻挡气缸1; set => SetProperty(ref cM_阻挡气缸1, value); }
        public CylinderDebugViewModel CM_阻挡气缸2 { get => cM_阻挡气缸2; set => SetProperty(ref cM_阻挡气缸2, value); }
        public CylinderDebugViewModel CM_左右气缸1 { get => cM_左右气缸1; set => SetProperty(ref cM_左右气缸1, value); }
        public CylinderDebugViewModel CM_左右气缸2 { get => cM_左右气缸2; set => SetProperty(ref cM_左右气缸2, value); }
        public CylinderDebugViewModel CM_前后气缸1 { get => cM_前后气缸1; set => SetProperty(ref cM_前后气缸1, value); }
        public CylinderDebugViewModel CM_前后气缸2 { get => cM_前后气缸2; set => SetProperty(ref cM_前后气缸2, value); }
        public CylinderDebugViewModel CM_上下气缸1 { get => cM_上下气缸1; set => SetProperty(ref cM_上下气缸1, value); }
        public CylinderDebugViewModel CM_上下气缸2 { get => cM_上下气缸2; set => SetProperty(ref cM_上下气缸2, value); }
        public bool IsRunning { get => isRunning; set => SetProperty(ref isRunning, value); }
        public double Speed { get => speed; set => SetProperty(ref speed, value); }
        public GongLiaoStationOptionViewModel(IServiceProvider service, Inspector inspector) : base(service)
        {
            GongLiaoStation = inspector.GongLiaoStation;
            this._inspector = inspector;
            _station = inspector.BuJinPiDai;
            PMoveCommand = _commandFactory.Create(PositiveMove);
            NMoveCommand = _commandFactory.Create(NositiveMove);
            StopCommand = _commandFactory.Create(Stop);
            CM_阻挡气缸1 = new(GongLiaoStation.Cylinder_阻挡气缸1) { Name = "阻挡气缸" };
            CM_阻挡气缸2 = new(GongLiaoStation.Cylinder_阻挡气缸2) { Name = "阻挡气缸2" };
            CM_左右气缸1 = new(GongLiaoStation.Cylinder_左右气缸1) { Name = "左右气缸" };
            CM_左右气缸2 = new(GongLiaoStation.Cylinder_左右气缸2) { Name = "左右气缸2" };
            CM_前后气缸1 = new(GongLiaoStation.Cylinder_前后气缸1) { Name = "前后气缸" };
            CM_前后气缸2 = new(GongLiaoStation.Cylinder_前后气缸2) { Name = "前后气缸2" };
            CM_上下气缸1 = new(GongLiaoStation.Cylinder_上下气缸1) { Name = "上下气缸" };
            CM_上下气缸2 = new(GongLiaoStation.Cylinder_上下气缸2) { Name = "上下气缸2" };
        }

        private void Stop()
        {
            GongLiaoStation.PiDai.AxisStop();
            IsRunning = false;
        }

        private void PositiveMove()
        {
            GongLiaoStation.PiDai.AxisPositiveMove(new VelocityCurve(0,Speed,0.1,0.1), default);
            IsRunning = true;
        }

        private void NositiveMove()
        {
            GongLiaoStation.PiDai.AxisNagetiveMove(Speed, default);
            IsRunning = true;
        }
    }
}
